import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MarqueeContinuous extends StatefulWidget {
  @override
  _MarqueeContinuousState createState() => _MarqueeContinuousState();
  double width;
  double height;
  List<Widget> children;
  Duration duration;
  double stepOffset;
  int count;
  Color color;
  Duration stop;
  int item = 0;

  MarqueeContinuous(
      {@required this.count,
      @required this.width,
      @required this.height,
      @required this.children,
      this.duration = const Duration(seconds: 3),
      // this.stepOffset = 10,
      // this.stop = const Duration(seconds: 1),
      this.color = Colors.transparent})
      : assert(width != null),
        assert(children != null),
        assert(height != null);
}

class _MarqueeContinuousState extends State<MarqueeContinuous> {
  PageController _controller;
  Timer _timer;
  // var _offset = 0.0;
  int _index = 0;
  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: 0);
    startScroll();
  }

  void startScroll() {
    _timer = Timer.periodic(widget.duration, _scroll);
  }

  void _scroll(timer) {
    _index++;
    _controller.animateToPage(
      _index % widget.children.length,
      duration: Duration(milliseconds: 500), //跳转的间隔时间
      curve: Curves.fastOutSlowIn, //跳转动画
    );
  }

  @override
  void dispose() {
    _timer?.cancel();

    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // color: Colors.white,
      decoration: BoxDecoration(
        color: Colors.white,
        
      ),
      width: widget.width,
      height: widget.height,
      margin: EdgeInsets.only(left: ScreenUtil().setWidth(15.0),right: ScreenUtil().setWidth(15.0)),
      // child: PageView.builder(
      //   controller: _controller,
      //   scrollDirection: Axis.vertical,
      //   itemBuilder: (BuildContext context, int index) {
      //     return widget.child;
      //   },
      // ),
      child: PageView(
        controller: _controller,
        scrollDirection: Axis.vertical,
        children: widget.children,
      ),
    );
  }
}
